package com.zhuifeng.orders.service.impl;

import com.zhuifeng.common.core.constant.CacheConstants;
import com.zhuifeng.common.core.domain.R;
import com.zhuifeng.common.core.utils.DateUtils;
import com.zhuifeng.common.core.utils.JwtUtils;
import com.zhuifeng.common.core.utils.StringUtils;
import com.zhuifeng.common.core.utils.snow.GuuidUtil;
import com.zhuifeng.common.redis.service.RedisService;
import com.zhuifeng.common.security.utils.SecurityUtils;
import com.zhuifeng.orders.domain.Site;
import com.zhuifeng.orders.mapper.SiteMapper;
import com.zhuifeng.orders.service.ISiteService;
import com.zhuifeng.system.domain.model.LoginUser;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description:
 * @author: 周国强
 * @time: 2022/8/19 19:45
 */
@Service
public class SiteServiceImpl implements ISiteService
{
    @Autowired
    private SiteMapper siteMapper;
    private final HttpServletRequest request;

    private final StringRedisTemplate redisTemplate;

    private final RedissonClient redissonClient;
    @Autowired
    private RedisService redisService;
    private final static String ACCESS_TOKEN = CacheConstants.LOGIN_TOKEN_KEY;

    public SiteServiceImpl(SiteMapper siteMapper, HttpServletRequest request, StringRedisTemplate redisTemplate, RedissonClient redissonClient) {
        this.siteMapper = siteMapper;
        this.request = request;
        this.redisTemplate = redisTemplate;
        this.redissonClient = redissonClient;
    }


    @Override
    public List<Site> selectSiteList() {
        //获取当前登录人
        LoginUser user=null;
        {
            String token = SecurityUtils.getToken(request);
            if (StringUtils.isNotEmpty(token)){
                String userKey = JwtUtils.getUserKey(token);
                user=redisService.getCacheObject(getTokenKey(userKey));
            }
        }
        return this.siteMapper.selectSiteList(user.getUserid()+"");
    }



    /**
     * 获取当前登录人信息
     * @return
     */
    @Override
    public LoginUser selectUser() {
        LoginUser user=null;
        {
            String token = SecurityUtils.getToken(request);
            if (StringUtils.isNotEmpty(token)){
                String userKey = JwtUtils.getUserKey(token);
                user=redisService.getCacheObject(getTokenKey(userKey));
            }
        }
        return user;
    }

    @Override
    public R<Site> selectUserSite() {
        LoginUser user=null;
        {
            String token = SecurityUtils.getToken(request);
            if (StringUtils.isNotEmpty(token)){
                String userKey = JwtUtils.getUserKey(token);
                user=redisService.getCacheObject(getTokenKey(userKey));
            }
        }

        Site site=this.siteMapper.selectUserSite(user.getUserid());
        return R.ok(site);
    }

    /**
     * 新增收货地址
     *
     * @param site 收货地址
     * @return 结果
     */
    @Override
    public int insertSite(Site site)
    {
        LoginUser user=null;
        {
            String token = SecurityUtils.getToken(request);
            if (StringUtils.isNotEmpty(token)){
                String userKey = JwtUtils.getUserKey(token);
                user=redisService.getCacheObject(getTokenKey(userKey));
            }
        }
//        if (site.getSiteState() == 1) {
//            siteMapper.updateUserIdState(user.getUserid()+"");
//        }
        site.setCreateTime(DateUtils.getNowDate());
        site.setSiteId(GuuidUtil.getUUID()+"");
        site.setUserId(user.getUserid()+"");
        site.setCreateBy(user.getUsername());
        return siteMapper.insertSite(site);
    }

    @Override
    public void deleteSiteId(String siteId) {
        siteMapper.deleteSiteId(siteId);
    }

    @Override
    public Site selectSiteId(String siteId) {
        return siteMapper.selectSiteId(siteId);
    }

    @Override
    public void updateSite(Site site) {
        siteMapper.updateSite(site);
    }

    @Override
    public void updateState(List<Site> list) {
        siteMapper.updateState(list);
    }

    @Override
    public void defultSiteById(String siteId) {

        LoginUser user=null;
        {
            String token = SecurityUtils.getToken(request);
            if (StringUtils.isNotEmpty(token)){
                String userKey = JwtUtils.getUserKey(token);
                user=redisService.getCacheObject(getTokenKey(userKey));
            }
        }

        List<Site> sites = siteMapper.selectSiteList(String.valueOf(user.getUserid()));
        for (Site site : sites) {
            if (site.getSiteId().equals(siteId)) {
                site.setSiteState(Long.parseLong("1"));
                siteMapper.defultSiteById(site);
            } else {
                site.setSiteState(Long.parseLong("2"));
                siteMapper.defultSiteById(site);
            }
        }
    }

    /**
     * 获取token
     * @param token
     * @return
     */
    private String getTokenKey(String token)
    {
        return ACCESS_TOKEN + token;
    }
}
